package com.supermap.business.cloud.api;
import com.alibaba.dubbo.config.annotation.Reference;
import com.supermap.dataservice.redis.JedisComponent;
import com.supermap.dataservice.util.JSONUtils;
import com.supermap.dubbo.api.cloud.api.ApiUserService;
import com.supermap.dubbo.api.cloud.cust.role.TreeNodeList;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * @author aqua
 * @desc
 * @date 2019年05月27 15:35
 * @email 1301584033@qq.com
 */
@Controller
@RequestMapping("/cloud/api")
public class ApiUserController {
    @Reference(timeout = 100000)
    ApiUserService apiUserService;

    @Autowired
    JedisComponent jedisComponent;


    @GetMapping("/apiLogin")
    @ResponseBody
    public JSONUtils apiLogin(String loginName, String pwd){
        return  apiUserService.apiLogin(loginName,pwd);
    }

    //验证
    @GetMapping("/getToken")
    @ResponseBody
    public JSONUtils getToken(String appKey,String appScrect){
        String token=null;
        if(jedisComponent.exists(appKey)){
            token=jedisComponent.get(appKey);
        }
        Map<String,Object> map= new HashMap<>();
        if(Strings.isNotBlank(token)){
            map.put("token",token);
            map.put("validtime",jedisComponent.getKeyTime(appKey));
            return new JSONUtils().setData(map);
        }
        JSONUtils jsonUtils=apiUserService.getToken(appKey,appScrect);
        if(jsonUtils.getCode()==200){
            //设置缓存
            jedisComponent.set(appKey,(String)jsonUtils.getData());
            //设置时效 保存半个小时
            jedisComponent.setKeyTime(appKey,7200);
            //设置缓存
            jedisComponent.set((String)jsonUtils.getData(),appKey);
            //设置时效 保存半个小时
            jedisComponent.setKeyTime((String)jsonUtils.getData(),7200);
            map.put("token",jsonUtils.getData());
            map.put("validtime",7200);
            return new JSONUtils().setData(map);
        }
        return new JSONUtils().setMsg("验证失败!请确人appKey和appScrect是否正确").setIsSuccess(false).setCode(100);
    }
    @GetMapping("/getApIUserTreeNodeList")
    @ResponseBody
    public JSONUtils getApIUserTreeNodeList(Integer menuId){
        TreeNodeList treeNodeList=new TreeNodeList();
        treeNodeList.setName("全部用户");
        treeNodeList.setOpen(true);
        List<TreeNodeList> lists=new ArrayList<>();
        List<Map<String,Object>> list=(List<Map<String,Object>>)apiUserService.getApiUserList(null).getData();
        for (Map map:list){
          if((int)map.get("del_flg")==0){
              TreeNodeList treeNodeList1=new TreeNodeList();
              treeNodeList1.setName((String)map.get("loginName"));
              treeNodeList1.setId((int)map.get("id"));
              lists.add(treeNodeList1);
          }
        }
        treeNodeList.setChildren(lists);
        return  new JSONUtils().setData(treeNodeList);
    }
    //修改appkey,appsecret
    @PostMapping("/upAuth")
    @ResponseBody
    public JSONUtils upAuth(String loginName){
        return  apiUserService.upAuth(loginName);
    }


    //添加用户
    @PostMapping("/addApiUser")
    @ResponseBody
    public JSONUtils  addApiUser(String loginName, String password,String addLoginName){
        return  apiUserService.addApiUser(loginName,password,addLoginName);
    }

    //删除用户
    @DeleteMapping("/delApiUser")
    @ResponseBody
    public JSONUtils delApiUser(Integer apiUserId,Integer del_flg){
        return  apiUserService.delApiUser(apiUserId,del_flg);
    }

    //查询用户
    @GetMapping("/getApiUserList")
    @ResponseBody
    public JSONUtils getApiUserList(String apiLoginName){
        return  apiUserService.getApiUserList(apiLoginName);
    }

    @PostMapping("/setApiUserMenu")
    @ResponseBody
    public JSONUtils setApiUserMenu(String dels,Integer apiuserId,String apimenuIds,String endTime){
        return  apiUserService.setApiUserMenu(dels,apiuserId,apimenuIds,endTime);
    }
}
